package com.neo.demo.controller;

import com.neo.demo.service.mailService;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Random;

@Controller
public class mailController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private mailService mailService;

    //常规的测试能否正常发送邮件
    @RequestMapping("/send")
    public String sendSimpleMail(Model model) {
            String validationCode = getValidationCode();
            mailService.sendSimpleMail("460030103@qq.com", "validationCode", validationCode);
            model.addAttribute("message", "完成");
            logger.info("测试simple完毕");

        return "defind";

    }

    //测试发送邮件，邮件格式是html格式
    @GetMapping("/sendHtml")
    public String sendHtmlMail(Model model)throws Exception{
//        String content="<html>\n" +
//                "<body>\n" +
//                "    <h3>hello world ! 这是一封Html邮件!</h3>\n" +
//                "</body>\n" +
//                "</html>";

        String content = "<!DOCTYPE html>\n" +
                "<html lang=\"en\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title></title>\n" +
                "    <style>\n" +
                "        strong{\n" +
                "            color: #1a76f8;\n" +
                "        }\n" +
                "        p{\n" +
                "\n" +
                "        }\n" +
                "        p.indent{\n" +
                "            text-indent: 2em;\n" +
                "        }\n" +
                "        p.tips{\n" +
                "            color: #999999;\n" +
                "            font-size: 12px;\n" +
                "            border-top: 1px solid #eeeeee;\n" +
                "            padding-top: 30px;\n" +
                "            text-align: center;\n" +
                "        }\n" +
                "        .box{\n" +
                "            border: 1px solid #eeeeee;\n" +
                "            padding: 15px 30px;\n" +
                "            border-radius: 15px;\n" +
                "        }\n" +
                "       .img{\n" +
                "           text-align: center;\n" +
                "       }\n" +
                "       .img div{\n" +
                "           display: inline-block;\n" +
                "           margin: 15px 30px;\n" +
                "       }\n" +
                "       .text-right{\n" +
                "           text-align: right;\n" +
                "           padding-right: 30px;\n" +
                "       }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<p>尊敬的 <strong>no.0123@163.com</strong> ，您好！</p>\n" +
                "<p class=\"indent\">欢迎您访问注册湖口公安一企一警系统。以下是您注册的验证信息。</p>\n" +
                "<div class=\"box\">\n" +
                "    <p>登录邮箱：<strong>no.0123@163.com</strong></p>\n" +
                "    <p>验 证 码： <strong>2344</strong></p>\n" +
                "    <p>湖口公安“一企一警”警企联防服务新机制给企业提供更好的营商环境；请务必在10分钟内输入上面验证码完成注册流程。</p>\n" +
                "    <div class=\"img\">\n" +
                "        <div>\n" +
                "            <img src=\"\" alt=\"\">\n" +
                "            <p>湖口公安小程序</p>\n" +
                "        </div>\n" +
                "        <div>\n" +
                "            <img src=\"\" alt=\"\">\n" +
                "            <p>湖口公安公众号</p>\n" +
                "        </div>\n" +
                "    </div>\n" +
                "</div>\n" +
                "<p class=\"indent\">扫黑除恶，净化社会，构建和谐，创建平安。报警电话：110</p>\n" +
                "<p class=\"text-right\">---湖口县公安局</p>\n" +
                "<p class=\"tips\">深圳中讯智慧科技有限公司 提供技术支持</p>\n" +
                "\n" +
                "</body>\n" +
                "</html>\n";


        mailService.sendHtml("460030103@qq.com","make test now ",content);
        logger.info("测试完毕");
        model.addAttribute("message","html完成");
        return "defind";

    }
    //测试发送邮件带附件的
    @GetMapping("/sendAttachmentsMail")
    public String sendAttachmentsMail(Model model){
        String filePath="E:\\资料\\大数据平台上线部署环境和部署清单\\大数据平台上线部署环境和部署清单\\开发系统\\共享交换系统\\部署手册-基础库查询.docx";
        mailService.sendAttachmentsMail("460030103@qq.com", "主题：带附件的邮件", "有附件，请查收！", filePath);
        logger.info("测试完毕");
        model.addAttribute("message","html带附件测试完成");
        return "defind";
    }


    public  String getValidationCode() {
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        String uuid = new String();
        for (int i = 0; i < 4; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            uuid += ch;
        }
        return uuid;
    }
}
